package cn.mygweb.admin.modules.sys.controller;

import java.util.List;

import org.assertj.core.util.Arrays;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import cn.mygweb.admin.modules.sys.pojo.Attachment;
import cn.mygweb.common.controller.BaseController;
import cn.mygweb.common.pojo.JsonResult;

/** 
* <p>Title: AttachmentController.java</p>
* <p>Description: 附件管理控制层</p>  
* @author gxb 
* @date 2019年8月6日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Controller
@RequestMapping("/admin/sys/attachment")
public class AttachmentController extends BaseController{

	/**
	 * <p>Title: list</p>
	 * <p>Description: 访问附件页面</p>
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "admin/modules/sys/attachment";
	}
	
	/**
	 * <p>Title: getAttList</p>
	 * <p>Description: 查询附件列表</p>
	 * @return
	 */
	@RequestMapping("/getAttList")
	public List<Attachment> getAttList(){
		return attachmentService.selectList(new Attachment());
	}

	/**
	 * <p>Title: ajaxAttList</p>
	 * <p>Description: 异步查询附件列表</p>
	 * @return
	 */
	@RequestMapping("/ajaxAttList")
	@ResponseBody
	public JsonResult ajaxAttList() {
		List<Attachment> list = attachmentService.selectList(new Attachment());
		return JsonResult.ok(list);
	}
	
	/**
	 * <p>Title: selectByIds</p>
	 * <p>Description: 通过id查询附件列表</p>
	 * @param ids
	 * @return
	 */
	@RequestMapping("/selectByIds")
	@ResponseBody
	public JsonResult selectByIds(String ids) {
		if(null != ids && !StringUtils.isEmptyOrWhitespace(ids)) {
			try {
				String[] idArr = ids.split(",");
				List<Object> idList = Arrays.asList(idArr);
				List<Attachment> attList = attachmentService.selectByIds(idList);
				return JsonResult.ok(attList);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return JsonResult.errorMsg("获取附件失败");
	}
	
	/**
	 * <p>Title: deleteAtt</p>
	 * <p>Description: 删除附件信息</p>
	 * @param id
	 * @param delFile	是否删除磁盘上的附件文件
	 * @return
	 */
	@RequestMapping("/deleteAtt")
	@ResponseBody
	public JsonResult deleteAtt(String id, boolean delFile) {
		try {
			attachmentService.deleteById(id, delFile);
			return JsonResult.ok("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("删除失败");
		}
	}
	
	/**
	 * <p>Title: setDownloadCounts</p>
	 * <p>Description: 修改下载次数</p>
	 * @param id
	 * @param counts
	 */
	@RequestMapping("/setDownloadCounts")
	@ResponseBody
	public void setDownloadCounts(String id, Integer count) {
		try {
			Attachment attachment = attachmentService.selectById(id);
			attachment.setDownloadCounts(count);
			attachmentService.update(attachment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
